import {config} from "./http/config.js"
import httpUtil from "./http/httpUtil.js"
import { GET, POST, DEL } from "./http/httpUtil.js";
import store from "./store/index.js"
const uploadImage = require('./http/upimg/uploadAliyun.js');
export default {
	// 添加行为记录
	addBehaveLog(params) {
		params.channelId = uni.getStorageSync('lastChannelId') || 0;
		params.salesId = uni.getStorageSync('lastAdminId') || 0;
		params.daren = uni.getStorageSync('lastDarenName') || '';
		params.qrcodeId = uni.getStorageSync('lastQrcodeId') || 0;
		params.openid = uni.getStorageSync('openid') || '';
		POST('/app/qrcode/qrcode/addBehaveLog', params, res => {
			console.log('添加行为记录', res);
		})
	},

	/**
	 * 跳转再封装，不支持复杂传参。
	 */
	routerTo(path, type, anmin) {
		console.log(path)
		// 判断是否是tabbar
		if(type=="navigateTo"){
			uni.navigateTo({
			    url: path,
				animationType: anmin,
				animationDuration: 300
			});
		}
		if(type=="redirectTo"){
			uni.redirectTo({
			    url: path,
				animationType: anmin,
				animationDuration: 200
			});
		}
		if(type=="reLaunch"){
			uni.reLaunch({
			    url: path,
				animationType: anmin,
				animationDuration: 200
			});
		}
		if(type=="switchTab"){
			uni.switchTab({
			    url: path,
				animationType: anmin,
				animationDuration: 200
			});
		}
		if(type=="navigateBack"){
			uni.navigateBack({
			    delta: 1
			});
		}	
	},
	showModal(title, content, showCancel,cd){
		uni.showModal({
			title: title,
			content: content,
			showCancel: showCancel,
			success: function (res) {
				if(typeof cd == "function"){
					cd(res)
				}
			}
		})
	},
	/**提示框
	 *title(标题)
	 *icon(图标):  success，loading，none
	 *duration(延时): 0为不关闭, 毫秒数
	 *options(其它参数)
	 */
	toast(title, icon = 'none', options) {
		uni.showToast({
			title: title || '',
			icon: icon,
			duration: (options && options.duration) || 1500,
			image: (options && options.image) || '',
			mask: (options && options.mask) || true,
		});
	},
	// 微信自动登录
	wxLogin() {
		if (uni.getStorageSync('token')) {
			return;
		}
	},
	// 刷新token
	refreshToken() {
		const refreshToken = uni.getStorageSync('refreshToken');
		const that = this
		if (!refreshToken) {
			return;
		}
		POST('/app/user/login/refreshToken',{refreshToken:refreshToken},function(res){
			console.log('refreshToken',res)
			if (res.data.code==1000) {
				uni.setStorageSync('token', res.data.data.token);
				uni.setStorageSync('refreshToken', res.data.data.refreshToken);
				uni.setStorageSync('islogout', false)
			} else {
				uni.removeStorageSync('token');
				uni.removeStorageSync('refreshToken');
				// 检查是否已在登录页或允许未登录访问的页面，避免重复跳转
				const pages = getCurrentPages();
				const currentPage = pages[pages.length - 1];
				const currentPath = currentPage.route;
				// 允许未登录访问的页面列表：首页、店铺、我的
				const allowUnloginPages = ['pages/index/index2', 'pages/index/report', 'pages/index/mine'];
				if (currentPath !== 'pages/index/login' && !allowUnloginPages.includes(currentPath)) {
					that.routerTo('/pages/index/login', 'navigateTo', 'none');
				}
				return false;
			}
		})
	},
	// 获取微信openid
	setStorageOpenid(callback) {
		uni.login({
			success: (res) => {
				if (res.code) {
					// 向后台请求 openid
					POST('/app/user/login/getOpenid', { code: res.code }, function (res) {
						console.log('openid请求结果:', res.data);
						const openid = res.data.openid;
						uni.setStorageSync('openid', openid);
						if (res.data.token && res.data.refreshToken) {
							uni.setStorageSync('token', res.data.token);
							uni.setStorageSync('refreshToken', res.data.refreshToken);
							uni.setStorageSync('islogout', false)
						}
						console.log('callback', typeof callback == "function", callback)
						if (typeof callback == "function") {
							callback()
						}
					}, function (res) {
						console.error('获取临时登录凭证失败', res);
					})
				} else {
					console.error('获取临时登录凭证失败', res);
				}
			},
			fail: (err) => {
				console.error('调用 uni.login 失败:', err);
			}
		});
	}
}
